// pages/login/index.js
import { studentLogin,
  teacherLogin
 } from "../../api/manage";

Page({
  data: {
    userType:'student',
    accountType:'学号',
    account:'',
    password:''
  },


  onLogin() {
    wx.setStorageSync('userType', this.data.userType); 
    if(this.data.userType==='student'){
      studentLogin(this.data.account, this.data.password).then(res => {
        wx.showToast({
          title: res.msg,
          icon:'none'
        });
        if(res.code===200){
          wx.setStorageSync('userID', res.data.studentId); 
          wx.switchTab({
            url: '/pages/news/news' 
        });
       }})
    }else{
      teacherLogin(this.data.account, this.data.password).then(res => {
        wx.showToast({
          title: res.msg,
          icon:'none'
        });
        if(res.code===200){
          wx.setStorageSync('userID', res.data.teacherId); 
          wx.setStorageSync('teacherNo', res.data.teacherNo); 
          wx.setStorageSync('teacherName', res.data.name); 

          wx.switchTab({url: '/pages/manage/index'});
        }})
    }
  },
  onRadioChange: function(e) {
    // 更新用户类型
    this.setData({
      userType: e.detail.value,
      accountType: e.detail.value === 'student' ? '学号' : '工号'
    });
  },

  inputAcc:function(e){
    this.setData({
      account: e.detail.value
    })
  },
  inputPwd:function(e){
    this.setData({
      password: e.detail.value
    })
  }
});
